define(function(require, exports, module) {
	var plugins = require('plugins');
	J = jQuery;
	require('layer');
	require('bootstrap');
	require('bootstraponhover');
	require('jqueryform');
	require('jquery-ui');
	require('handlebars');
	require('waterfall');
	require('webuploader');
	require('drag');
	require('searchableSelect');

	layer.config({
	    path: '/assets/vendors/layer/' //layer.js所在的目录，可以是绝对目录，也可以是相对目录
	});
	var timenow = new Date().getTime();
	//用户登录后鼠标经过菜单
	$(".right-part").bootstrapDropdownOnHover();
	$(".menu-bar").bootstrapDropdownOnHover();
	//加载用户所有专辑
	var id=$("#albumWaterfall").attr("data-id");	
	$('#albumWaterfall').waterfall({
		itemCls: 'wfc', 
		fitWidthBoolean: false, //是否自适应父元素宽度
	    colWidth: 236,
	    maxCol: undefined,
	    gutterWidth: 16,
	    gutterHeight: 16,
	    maxPage: 10,
	    align:'left',
	    minCol: 1, 
	    bufferPixel: -50, 
	    containerStyle: {
	        position: 'relative'
	    },
	    resizable: true, 
	    isFadeIn: false,
	    isAnimated: false,
	    isFadeInBoolean:false,
	    isAutoPrefill: true,
	    checkImagesLoaded: true,
	    resizableBoolean: true, //缩放时是否触发数据重排
	    state: { 
	        isDuringAjax: false, 
	        isProcessingData: false, 
	        isResizing: false,
	        curPage: 2 
	      },
	    callbacks: {
	        loadingFinished: function($loading, isBeyondMaxPage) {
	            if ( !isBeyondMaxPage ) {
	                $loading.fadeOut();
	            } else {
	                $loading.hide();
	                $('#albumWaterfall').show();
	            }
	        },
	        renderData: function (data, dataType) {
	            var tpl,
	                template,
	                resultNum = data.data.pageCount;
	            
	            if (data.data.pageNum > resultNum) {
	                $('#albumWaterfall').waterfall('pause', function() {
	                    $('.waterfall-loading').html('<p style="color:#666;">数据加载完毕...</p>')
	                });
	                //alert(page); 
	                $('#albumWaterfall').after('<div class="loading" style="display: block;"><img src="/assets/images/end.png"></div>');
	                $('.waterfall-loading').hide();
	            }else{
	            	if ( dataType === 'json' ||  dataType === 'jsonp'  ) { // json or jsonp format
	            		var waterfalldeta="";
	            		$.each(data.data.list, function(k, v) {
	            			var imglist="",userlogin="";
	            			$.each(v.pinList, function(i, val) {
	            				var cover="";
	            				if(i==0){
	            					cover=" class=\'large\'";
		            			}
	            				imglist+="<img src=\'/thumb?src="+val.imgurl+"&amp;w=216&amp;h=216&amp;s=1\'"+cover+">";
	            			});	
	            			if(userid==""){//登录状态判断，未登录显示关注，登录显示编辑
		            			userlogin="<a data-id=\'"+v.albumId+"\' href=\'#\' onclick=\'return false;\' class=\'follow btn btn14 wbtn\'><strong> 关注</strong><span></span></a>";
		            		}else{
		            			if(userid==id){//登录状态下登录id和当前id是同一个就显示编辑，不是同一个就显示关注
		            				userlogin="<a href=\"/album/"+v.albumId+"/edit/\" class=\"btn btn14 wbtn\"><strong>编辑</strong><span></span></a>";
		            			}else{
		            				userlogin="<a data-id=\'"+v.albumId+"\' href=\'#\' onclick=\'return false;\' class=\'follow btn btn14 wbtn\'><strong> 关注</strong><span></span></a>";
		            			}
		            		}
	            			waterfalldeta += "<div class=\'Board wfc inited\' style=\'position: absolute; left: 260px; top: 0px;\'><div class=\'draglay\'></div><a href=\'/album/"+v.albumId+"\' target=\'_blank\' class=\'link  x\'>" +
	            					imglist+
	            					"<div class=\'shadows\'><div class=\'large-shadow\'></div><div class=\'shadow\'></div><div class=\'shadow\'></div><div class=\'shadow\'></div></div><div class=\'over empty-board\'><h3>"+v.albumname+"</h3><h4></h4><div class=\'pin-count\'>"+v.countPin+"</div></div></a><div class=\'FollowBoard\'>"+userlogin+"</div></div>";
	            		});
	            		$('#albumWaterfall').append(waterfalldeta);
	            		return;
		            } else {
		                return data;
		            }
	            }
	        }
	    },
	    path: function(page) {
	        return '/album/album_view.json?uid='+id+'&p=' + page;
	    }
	});	
	
	//加载用户所有收藏图片
	var uid=$("#waterfall").attr("data-id");
	$("#waterfall").waterfall({
		itemCls: 'pin', 
	    colWidth: 236,  
	    gutterWidth: 15,
	    gutterHeight: 15,
	    maxPage: 10,
	    bufferPixel: -50, 
	    auto_imgHeight:false,
	    align:'left',
	    resizable: true,
	    checkImagesLoaded: true,
	    callbacks: {
	        loadingFinished: function($loading, isBeyondMaxPage) {
	            if ( !isBeyondMaxPage ) {
	                $loading.fadeOut();
	            } else {
	                $loading.hide();
	                $('.waterfall').show();
	            }
	        },
	        renderData: function (data, dataType) {
	            var tpl,
	                template,
	                resultNum = data.data.pageCount;
		            
		        if (data.data.pageNum > (resultNum+1)) {
	                $('#waterfall').waterfall('pause', function() {
	                    $('.waterfall-loading').html('<p style="color:#666;">数据加载完毕...</p>')
	                });
	                $('#waterfall').after('<div class="loading" style="display: block;"><img src="/assets/images/end.png"></div>');
	                $('.waterfall-loading').hide();
	            }else{
		            if ( dataType === 'json' ||  dataType === 'jsonp'  ) { // json or jsonp format
		            	var waterfalldeta="";
		            	$.each(data.data.list, function(k, v) {
		            		waterfalldeta += "<div class=\"pin wfc\" style=\"position: absolute; left: 256px; top: 474px;\"><div class=\"actions\"><div class=\"right\"><a data-id=\""+v.pinId+"\" title=\"喜欢\" href=\"#\" onclick=\"return false;\" class=\"like btn-with-icon btn btn14\"><i class=\"heart\"></i><span class=\"text\"> </span></a></div><div class=\"left\"><a data-id=\""+v.pinId+"\" href=\"#\" onclick=\"return false;\" class=\"repin btn btn14 rbtn\"><span class=\"text\"> 收藏</span></a></div></div><a href=\"/pin/"+v.pinId+"/\" target=\"_self\" rel=\"\" class=\"img x layer-view loaded\"><div class=\"default-bg\"></div><img src=\"/thumb?src="+v.imgurl+"&amp;w=224\"><span style=\"display: none\" class=\"stop\"></span><div class=\"cover\"></div></a><p class=\"description\">"+v.content+"</p><div class=\"convo attribution clearfix\"><a href=\"/u/"+v.userId+"\" title=\""+v.nickname+"\" rel=\"nofollow\" class=\"img x\"><img src=\""+v.face+"\" class=\"avt\"></a><div class=\"text\"><div class=\"inner\"><div class=\"line\"><a href=\"/u/"+v.userId+"\" rel=\"\" class=\"author x\">"+v.nickname+"</a>&nbsp;发布于</div><div class=\"line\"><a href=\"/category/"+v.categoryUrl+"/\" rel=\"\" class=\"x\">"+v.categoryName+"</a>&nbsp;频道</div><a title=\"评论\" data-name=\""+v.nickname+"\" data-id=\""+v.pinId+"\" user-id=\""+v.userId+"\" id=\"addreview\" class=\"replyButton\"></a></div></div></div><div style=\"\" class=\"comments muted\"></div></div>";
		            	});
	            		$('#waterfall').append(waterfalldeta);
	            		return;
		            } else { // html format
		                return data;
		            }	
	            }
	        }
	    },
	    path: function(page) {
	        return '/user/pins_list.json?id='+uid+'&p=' + page;
	    }
	});
	

	$(".followuser").on("click", function(){
		var uid=$(this).attr("data-id");
		$.ajax({
    		url: '/u/userfollow.json?'+timenow, 
    		data: {'userByid': uid},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				$(this).children(".text").html("已关注");
    				layer.msg(data.message, {icon: 1});
    				window.location.reload();
    			}else{
    				$(this).children(".text").html("关注");
    				layer.msg(data.message, {icon: 2});
    			}
          	}
    	});
	});
	
	//打开私信窗口
	$(document).on('click', '.msg', function (){
		var touid=$(this).attr("data-id");
		var uname=$(this).attr("data-uname");
		$(".with-user-name").html(uname);
		$("#messages-box").html("");
		$("#touid").val(touid);
		$("#dm_box").show();
		$(".dialog-view").show();
		$(".dialog-overlay").show();
		$.get("/message/list.json?"+timenow, { touid: touid },function(data){
			if(data.data!=null){
				var nums = data.data.sort(function(a,b){
					return b.row_number-a.row_number;
				})
				$.each(nums, function(k, v) {
					var touser=null;
					if(touid==v.fromid){
						touser=" received";
					}else{
						touser=" sent";
					}
					$("#messages-box").append("<div class=\"message clearfix"+touser+"\"><div class=\"ms-time\"><div class=\"ms-time-text\">"+v.sendtime+"</div></div><a href=\"/"+v.fromid+"/\"><img src=\""+v.fromface+"\" class=\"avt\"></a><div class=\"text\">"+v.message+"<svg class=\"triangle\"><polygon points=\"0 0, 10 0, 10 10\"></polygon></svg></div></div>");
				});
			}
			$("#messages-box").scrollTop(1800);
		});
	});
	
	//发送短信
	$(".send").on("click", function(){
		var touid=$("#touid").val();
		var message=$(".clear-input").val();
		var sendtime=$("#sendtime").val();
		var state=$("#state").val();
		if(message.length==0){
			layer.msg('消息内容不能为空', {icon: 2});
			return;
		}
		$.ajax({
    		url: '/member/addmessage.json?'+timenow, 
    		data: {'touid': touid,'message': message,'sendtime': sendtime,'state': state},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				//layer.msg(data.message, {icon: 1});
    				$("#messages-box").append("<div class=\"message clearfix sent\"><div class=\"ms-time\"><div class=\"ms-time-text\">"+data.data.sendtime+"</div></div><a href=\"/"+data.data.fromid+"/\"><img src=\""+data.data.fromface+"\" class=\"avt\"></a><div class=\"text\">"+data.data.message+"<svg class=\"triangle\"><polygon points=\"0 0, 10 0, 10 10\"></polygon></svg></div></div>");
    				$("#messages-box").scrollTop(1800+$("#messages-box").height());
    				$(".clear-input").val("");
    				$(".send").addClass(" disabled");
    				//window.location.reload();
    			}else{
    				layer.msg(data.message, {icon: 2});
    			}
          	}
    	});
	});
	
	$(document).on("keyup", ".clear-input", function (){
		var text=$(".clear-input").val();
		if(text.length>1){
			$(this).next(".send").removeClass('disabled');
		}else{
			$(this).next(".send").addClass('disabled');
		}
	});
	
	$(document).on("click", ".close-btn", function (){
		$("#dm_box").hide();
	});
	
	//屏蔽联系人
	$(document).on("click", ".action", function (){
		$(".action").addClass('active');
	});
	
	//鼠标离开后点击其他区域隐藏
	$(document).bind("click",function(e){ 
		var target  = $(e.target); 
	    if(target.closest(".active").length == 0){ 
	    	$(".action").removeClass("active");
	    }else{ 
	    	$(".action").addClass("active");
	    } 
	});
	
	//用户添加专辑分类
	$(document).on("click", ".add-board", function (){
		$(".dialog-overlay").show();
		$("#CreateBoard").show();
	});
	
	//关闭添加专辑分类
	$(document).on("click", ".close-btn, .reject", function (){
		$(".dialog-overlay").hide();
		$("#CreateBoard").hide();
	});
	
	//点击其他区域收缩分类菜单
	$(document).bind("click",function(e){ 
		var target = $(e.target); 
		if(target.closest(".BoardSelector").length == 0){ 
			$(".BoardList").hide(); 
		} 
	});
	
	//点击下来菜单
	$(document).on('click', '.CurrentBoard', function (){
		$(".CurrentBoard").parent().parent().children(".BoardList").show(); 
	});
	
	$(document).on('click', '.BoardCategory', function (){
		$(this).parent().parent().parent(".BoardList").hide();
		$(this).parent().parent().parent().prev().children(".CurrentBoard").html($(this).text()); 
		var classId=$(this).attr("data-id");
		$("#classId").val(classId);
	});
	
	//添加专辑
	$(".action").on("click", function(){
		var albumname=$("#albumname").val();
		var classId=$("#classId").val();
		$.ajax({
    		url: '/member/album/add.json?'+timenow, 
    		data: {'albumname':albumname,'classId':classId},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				layer.msg(data.message, {icon: 1});
    				window.location.reload();
    			}else{
    				layer.msg(data.message, {icon: 2});
    			}
          	}
    	});
	});
	
	//
	$("#albumEdit").ajaxForm({
		dataType : 'json',
		success : function(data) {
			if (data.code==0) {
				layer.msg(data.message, {icon: 1});
				window.location.href = data.url;
			}else{
				layer.msg(data.message, {icon: 2});
				return false;
			}
		}
	});
	
	
	$(document).on('click', '.btn-del', function (){
		layer.confirm('您确定要删除本专辑吗？', {
			btn: ['确定','取消'] //按钮
		}, function(){
			var id=$("#album_id").val();
			$.ajax({
	    		url: '/album/delete.json?'+timenow, 
	    		data: {'id':id},
	    		dataType: "json",
	    		type :  "POST",
	    		cache : false,
	    		async: false,
	    		error : function(i, g, h) {
	    			layer.msg('请登录后操作', {icon: 2});
	    		},
	    		success: function(data){	
	    			if (data.code==0) {
	    				layer.msg(data.message, {icon: 1});
	    				window.location.href = data.url;
	    			}else{
	    				layer.msg(data.message, {icon: 2});
	    			}
	          	}
	    	});
		}, function(){
		   
		});
	});
	
	//收藏图片编辑
	$('#tags').tagit({
        singleField: true,
        singleFieldNode: $('#fieldTags'),
        tagLimit: 5
    });
	//专辑名下拉选择
	$('#albumId').searchableSelect();
	$(document).on('click', '#pin_edit', function (){
		var tags=$("#fieldTags").val();
		var content=$("#description_pin_edit").val();
		var albumId=$("#albumId").val();
		var oldAlbumId=$("#oldAlbumId").val();
		var pinId=$("#pinId").val();
		if(content.length<4){
			layer.msg('描述不得少于4个字符', {icon: 2});
			return;
		}
		$.ajax({
    		url: '/pin/updatepin.json?'+timenow, 
    		data: {'content':content,'tags':tags,'albumId':albumId,'oldAlbumId':oldAlbumId,'pinId':pinId},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				layer.msg(data.message, {icon: 1});
    				window.location.href = data.url;
    			}else{
    				layer.msg(data.message, {icon: 2});
    				return;
    			}
          	}
    	});
	});
	//删除图片收藏信息
	$(document).on('click', '.btn-del', function (){
		layer.confirm('您确定要从本专辑中删除此图吗？', {
			btn: ['确定','取消'] //按钮
		}, function(){
			var albumId=$("#albumId").val();
			var pinId=$("#pinId").val();
			$.ajax({
	    		url: '/pin/delete.json?'+timenow, 
	    		data: {'albumId':albumId,'pinId':pinId},
	    		dataType: "json",
	    		type :  "POST",
	    		cache : false,
	    		async: false,
	    		error : function(i, g, h) {
	    			layer.msg('请登录后操作', {icon: 2});
	    		},
	    		success: function(data){	
	    			if (data.code==0) {
	    				layer.msg(data.message, {icon: 1});
	    				window.location.href = data.url;
	    			}else{
	    				layer.msg(data.message, {icon: 2});
	    				return;
	    			}
	          	}
	    	});
		}, function(){
		   
		});
	});
});